# Copyright © 2014 Jesse 'Jeaye' Wilkerson
# See licensing in LICENSE file, or at:
#   http://www.opensource.org/licenses/MIT
#
# File: Makefile.in
# Author: Jesse 'Jeaye' Wilkerson

SOURCES=src/main.cpp

OBJ_DIR=obj/
CPP_OBJECTS=$(SOURCES:.cpp=.cpp.o)
OBJECTS=$(CPP_OBJECTS)
BIN_OBJECTS=$(foreach obj,$(OBJECTS), $(addprefix $(OBJ_DIR), $(obj)))
BIN=bin/stdman

# Generation/Installation
PREFIX=%PREFIX%
DATAROOTDIR=%DATAROOTDIR%
MANDIR=%MANDIR%
REFERENCE=%REFERENCE%
TMP_MAN=%TMP_MAN%
THREADS=%THREADS%

CXX=c++
CXX_WARN=-Wall -Wextra -pedantic
CXX_NOWARN=
CXX_OPT=-O3
CXX_INCLUDE=-I src
CXX_PLATFORM_FLAGS=%CXX_PLATFORM_FLAGS%
CXX_FLAGS=-std=c++11 $(CXX_WARN) $(CXX_NOWARN) $(CXX_OPT) $(CXX_INCLUDE) $(CXX_PLATFORM_FLAGS) -c

LD_PLATFORM_LIBS=%LD_PLATFORM_LIBS%
LD_LIBS=-lcurl $(LD_PLATFORM_LIBS)

STDMAN_VERSION=$(shell date +"%Y.%m.%d")
CXX_FLAGS:=$(CXX_FLAGS) -DSTDMAN_VERSION=$(STDMAN_VERSION)

.PHONY: all setup clean stdman stdman_setup stdman_link generate install

.SILENT:

all: stdman
	echo "Success"

setup: clean
	echo "Preparing"
	mkdir -p $(OBJ_DIR) bin man tmp

clean:
	rm -f bin/stdman
	rm -rf $(OBJ_DIR)
	echo "Cleaned"

stdman: stdman_setup stdman_link
	echo "Done building stdman"

stdman_setup: setup
	echo "Building stdman"

stdman_link: stdman_setup $(OBJECTS)
	echo "  Linking"
	$(CXX) -o $(BIN) $(BIN_OBJECTS) $(LD_LIBS)

%.cpp.o: %.cpp stdman_setup
	echo "  Compiling $<"
	mkdir -p $(addprefix $(OBJ_DIR),$(dir $<))
	$(CXX) $(CXX_FLAGS) $< -o $(addsuffix .o,$(addprefix $(OBJ_DIR), $<))

generate: stdman
	export tmp_man=$(TMP_MAN) &&\
	export reference=$(REFERENCE) &&\
	export threads=$(THREADS) &&\
	./do_generate
	rm -f $(TMP_MAN)/*.html

install:
	export tmp_man=$(TMP_MAN) &&\
	export reference=$(REFERENCE) &&\
	export mandir=$(DESTDIR)$(MANDIR) &&\
	./do_install
